//app.js
var util = require('./utils/util.js');
App({
  data:{
    app_server: 'http://127.0.0.1:8888',// 'https://yunwei.bjjtfc.com.cn',//http://127.0.0.1:8000  https://yunwei.bjjtfc.com.cn   https://yunwei.gongsioa.com
    token:'',
    noLoadMoreText:'已经到底部了',
    defaultLoadMoreText:'暂无数据',
    selPerson:{"id":'',"userName":''},//选择处理人
    limit:15,//每页条数
    unreadMsgCount:0,//未读信息数量
    undoCheckCount:0,//待巡检数量
    undoWorkCount:0,//待处理工单
    permissions:[],//用户权限，同pc端
    userId:null,
    loginState:[]//观察者模式，绑定手机或者解绑都会执行里面的方法
  },
  //初始化token信息
  initToken: function () {
    var that = this;
    that.data.token = wx.getStorageSync("token") || ''; //获取token
    that.data.userId = wx.getStorageSync("userId") || ''; //获取token
    that.data.permissions = wx.getStorageSync("permissions") || '';//获取用户权限
    if (that.data.token) {//如果有值则验证一下token是否有效
      wx.request({
        url: that.data.app_server + "/weixin/tokenCheck",
        data: {
          token: that.data.token
        },
        success: function (res) {
          if (!res.data.msg) {//无效
            that.data.token = '';
            wx.removeStorageSync('token');
            wx.removeStorageSync('userId');
            wx.removeStorageSync('permissions');
            wx.showModal({
              title: '提示',
              content: '身份信息过期,请重新登录',
              showCancel: false
            })
          }
        }
      })
    }
  },

  getPermissions(that){
    that.data.userId = wx.getStorageSync("userId") || ''; //获取token
    if (that.data.userId){
      wx.request({
        url: that.data.app_server + "/weixin/getPermissions",
        data: {
          userId: that.data.userId
        },
        success: function (res) {
          wx.removeStorageSync('permissions');
          wx.setStorageSync("permissions", res.data.msg);//将权限写入手机
          that.data.permissions = res.data.msg;
        }
      })
    }
  },
  onLaunch: function () {
    this.initToken();
  },
  //当小程序启动，或从后台进入前台显示，会触发 onShow
  onShow:function(){
    this.getPermissions(this);
  },
  globalData: {
    userInfo: null
  }
})